package models.production;

import com.avaje.ebean.Page;
import dao.TaskManager;
import models.core.BaseAttrSpec;
import models.core.CompositeAttrSpec;
import models.core.GipxyModel;
import models.inventory.ResourceSpec;
import play.data.validation.Constraints;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: hungdq
 * Date: 4/21/12
 * Time: 10:25 AM
 * To change this template use File | Settings | File Templates.
 */
@Entity
public class TaskSpec extends GipxyModel{
    @Constraints.Required
    public String name;
    @ManyToMany
    public List<BaseAttrSpec> attrSpecs = new ArrayList<BaseAttrSpec>();

    @ManyToMany(cascade = CascadeType.ALL)
    public List<CompositeAttrSpec> compositeAttrSpecs;

    @ManyToMany(cascade=CascadeType.ALL)
    public List<ResourceSpec> resourceSpecs = new ArrayList<ResourceSpec>();

    //phoi = nguyen lieu chinh dau vao
    //public String phoi;

    public List<ResourceSpec> getResourceSpecs() {
        return resourceSpecs;
    }

    public void setResourceSpecs(List<ResourceSpec> resourceSpecs) {
        this.resourceSpecs = resourceSpecs;
    }

    /**
     * Generic query helper for entity Computer with id Long
     */
    public static Finder<Long,TaskSpec> find = new Finder<Long,TaskSpec>(Long.class, TaskSpec.class);

    /**
     * Return a page of computer
     *
     * @param page Page to display
     * @param pageSize Number of computers per page
     * @param sortBy Computer property used for sorting
     * @param order Sort order (either or asc or desc)
     * @param filter Filter applied on the name column
     */
    public static Page<TaskSpec> page(int page, int pageSize, String sortBy, String order, String filter) {
        System.out.println("bbbb:"+filter);
        return find.where()
                        //.ilike("name", "%" + filter + "%")
                        .orderBy(sortBy + " " + order)
                        .findPagingList(pageSize)
                        .getPage(page);
    }

    public String getAttributeNames(){
        /*String result = "";
        for (ProcessTaskAttribute processTaskAttribute: processTaskAttributes){
            result += processTaskAttribute.attribute.name+ ",";
        }

        return (result.endsWith(",")?result.substring(result.length()-1):result);*/
        return TaskManager.ProcessTaskAttributeNames(this);
        //return "att1, att2, att3";
    }


}
